/*

This file makes table 3.

*/
global writeout 0 // change to 1 for writing out, 0 to not
global quietly quietly // comment out for noisy

$quietly { // top top quietly
  clear
  set more off
  set seed 1337
  set sortseed 1337

  capture cd "~/Desktop/RESTAT_CODE"

  global dir_root = "."
  global dir_code = "${dir_root}/code"
  global dir_data = "${dir_root}/data"
  global dir_output "${dir_root}/output"

  // Load the bootstrap data
  /* quietly do "${dir_code}/1.1_cache-load_lasso_data.do" */


  // From table 2 file - we used scalar variable, but we can't assume the user will run all tables sequentially.
  scalar lambda = 16.354
  scalar lambda_ci5 = 16.241
  scalar lambda_ci95 = 16.468
  scalar lambda_ci5 = 15.450
  scalar lambda_ci95 = 17.259


  import delimited "${dir_data}/daily.csv", clear
  import delimited "${dir_data}/all_states_daily.csv", clear

  scalar july1 = td(01july2020)
  scalar april24 = td(24april2020)
  scalar IN_start = td(25april2020)
  scalar IN_end = td(29april2020)
  scalar UT_start = td(01may2020)
  scalar UT_end = td(01july2020)

  tostring date, replace
  gen yr = substr(date, 1, 4)
  gen mo = substr(date, 5, 2)
  gen dy = substr(date, 7, 2)
  foreach item in dy mo yr{
    destring `item', replace
  }

  drop date
  gen date = mdy(mo, dy, yr)
  format date %td

  gen pos_rate = positiveincrease/totaltestresultsincrease


  rename positive ccases
  rename state state2

  merge m:1 state2 using "${dir_data}/state fips.dta", nogen
  merge m:1 statename using "${dir_data}/state_populations.dta", nogen

  *drop non-states
  drop if statename==""
  drop if statename=="American Samoa"
  drop if statename=="Guam"
  drop if statename=="Northern Mariana Islands"
  drop if statename=="Puerto Rico"
  drop if statename =="Virgin Islands"
  drop if statename=="District of Columbia"


  sort statename date, stable
  by statename: gen pos_incr_ma7 = (positiveincrease+positiveincrease[_n-1]+positiveincrease[_n-2]+positiveincrease[_n-3]+positiveincrease[_n-4]+positiveincrease[_n-5]+positiveincrease[_n-6])/7
  by statename: gen totaltest_incr_ma7 = (totaltestresultsincrease+totaltestresultsincrease[_n-1]+totaltestresultsincrease[_n-2]+totaltestresultsincrease[_n-3]+totaltestresultsincrease[_n-4]+totaltestresultsincrease[_n-5]+totaltestresultsincrease[_n-6])/7
  gen pos_rate_ma7 = pos_incr_ma7/totaltest_incr_ma7

  encode state2, gen(state_n)
  xtset state_n date

  bysort state_n: gen recov_calc = sum(positiveincrease)
  gen recov_calc_lag = l21.recov_calc - death
  replace recovered = recov_calc_lag

  gen pI = (pos_rate_ma7)/(`=lambda' * (1 - pos_rate_ma7) + pos_rate_ma7)

  quietly sum pos_rate_ma7 if date ==`=july1'
  local posRate_all = r(mean)
  local pI5_all=`posRate_all' / (`=lambda_ci95' * (1-`posRate_all') + `posRate_all')
  local pI95_all=`posRate_all' / (`=lambda_ci5' * (1-`posRate_all') + `posRate_all')


  gen pos_rate2 = ccases/totaltestresults
  gen prev_public_ = ccases/ pop
  gen prev_public1_ = (ccases-death)/ pop
  gen prev_public2_ = (ccases-death-recovered)/ pop
  gen est_case_ratio = pI / prev_public2_

  /* Write out dataset for figures */
  if $writeout save "${dir_data}/horseracedata", replace

  levelsof state2, local(states)
  local states : list sort states

  foreach state of local states {
    quietly sum pos_rate_ma7 if date ==`=july1' & state2 == "`state'"
    local posRate_`state' = r(mean)
    local pI5_`state'=`posRate_`state'' / (`=lambda_ci95' * (1-`posRate_`state'') + `posRate_`state'')
    local pI95_`state'=`posRate_`state'' / (`=lambda_ci5' * (1-`posRate_`state'') + `posRate_`state'')

    noisily display "`state' ", _c
    foreach v in pI pos_rate_ma7 prev_public1_ prev_public2_ {
      quietly sum `v' if date ==`=july1' & state2 == "`state'"
      if r(mean) == . noisily display " &        ", _c
      else noisily display " & " %5.2f (r(mean)*100) "\%", _c
      if "`v'" == "pI" {
        noisily display " & [" %5.2f `pI5_`state''*100 ", " %5.2f `pI95_`state''*100 "] ", _c
      }
    }
    quietly sum est_case_ratio if date ==`=july1' & state2 == "`state'"
    if r(mean) == . noisily display " &       \\"
    else noisily display " & " %5.2f r(mean) " \\ "
  }
  // Average
  noisily display "\addlinespace" _n "\bf{AVERAGE}", _c
  foreach v in pI pos_rate_ma7 prev_public1_ prev_public2_ {
    quietly sum `v' if date ==`=july1'
    noisily display " & \bf{" %5.2f (r(mean)*100) "\%} ", _c
      if "`v'" == "pI" {
        noisily display " & [" %5.2f `pI5_all'*100 ", " %5.2f `pI95_all'*100 "] ", _c
      }
  }
  quietly sum est_case_ratio if date ==`=july1'
  noisily display " & \bf{" %5.2f r(mean) "} \\ "

 /* wsum pos_rate prev_public2_ pI if state2 == "UT" & (date >= `=UT_start') & (date <= `=UT_end') */
  sum prev_public2_ if (state2 == "UT") & (date >= `=UT_start') & (date <= `=UT_end'), d
  sum pos_rate if (state2 == "UT") & (date >= `=UT_start') & (date <= `=UT_end'), d
    local ut_posrate = round(r(p50), .0001)
  noisily display "UT median pos rate between May 1 and July 1: `ut_posrate'"
  noisily display "UT median pI between May 1 and July 1: `=`ut_posrate' / (round(`=lambda') * (1 - `ut_posrate') + `ut_posrate')'"

  /* wsum prev_public*_ if (state2 == "IN") & (date >= `=IN_start') & (date <= `=IN_end') */
  sum pos_rate if (state2 == "IN") & (date >= `=IN_start') & (date <= `=IN_end'), d
  noisily display "IN median pos rate between April 25 and 29: " r(p50)
  sum pI if (state2 == "IN") & (date >= `=IN_start') & (date <= `=IN_end'), d
  noisily display "IN median pI between April 25 and 29: " r(p50)


  /* positivity rates in Utah and SLC on March 3rd, 7 day moving average */

  scalar pos_slc = 8.6
  scalar pos_utah = 10.24
  scalar him_slc = 100*pos_slc/(`=lambda'*(100 - pos_slc)  + pos_slc)
  scalar him_utah = 100*pos_utah/(`=lambda'*(100 - pos_utah)  + pos_utah)
  noisily display "Utah/SL County median pos rates March 3-13: " %3.0f `=him_utah*100' "% / " %3.0f `=him_slc*100' "%"

  noisily display "NY on April 24th:"
  noisily list date state2 pI prev_public2_ est_case_ratio if (state2 == "NY") & (date == `=april24')

} // end top top $quietly
